String.prototype.startsWith = function (prefix) {
    return this.toLowerCase().indexOf(prefix.toLowerCase()) == 0;
};

String.prototype.filterPrefix = function (prefix) {
    if (this.startsWith(prefix)) {
        return this.substr(prefix.length);
    }
    return this;
};

Array.prototype.findIf = function (predicate) {
    var len = this.length;
    if (len > 0) {
        for (var i = 0; i < len; i++) {
            if (predicate(this[i])) {
                return this[i];
            }
        }
    }
    return null;
}

Array.prototype.indexOf = function (o) {
	for (var i = 0; i < this.length; i++) {
		if (this[i] === o) {
			return i;
		}
	}
	return -1;
};

Array.prototype.contains = function (o) {
	return this.indexOf(o) != -1;
};

var posTop = 50;
var posLeft = 50;

function debug(msg) {
    var el = document.createElement("SPAN");
    var text = document.createTextNode(msg);
    el.appendChild(text);

    el.style.position = "absolute";
    el.style.top = posTop;
    el.style.left = posLeft;

    posTop += 14;
    posLeft += 25;

    document.body.appendChild(el);
}

function escapeXml(value) {
	var s = (value ? value.toString() : "");
	var result = "";
	var c;

    var lastoff = 0;
    for (var i = 0; i < s.length; i++) {
        c = s.charCodeAt(i);

        if (c >= 0x20 && c < 0x80) {
            switch (c) {
            case 60: //<
                result += s.substring(lastoff, i) + "&lt;";
                lastoff = i + 1;
                break;

            case 62: //>
                result += s.substring(lastoff, i) + "&gt;";
                lastoff = i + 1;
                break;

            case 38: //&
                result += s.substring(lastoff, i) + "&amp;";
                lastoff = i + 1;
                break;

            case 34: //"
                result += s.substring(lastoff, i) + "&quot;";
                lastoff = i + 1;
                break;

            case 39: //'
                result += s.substring(lastoff, i) + "&apos;";
                lastoff = i + 1;
                break;
            default:
                result += s.charAt(i);
                lastoff = i + 1;
                break;
            }

        } else {
            result += s.charAt(i);
            lastoff = i + 1;
        }
    }

    return result + s.substring(lastoff, s.length);
}

function blockUI() {
    $.blockUI('<img src="' + getURL('/img/ajax-loader.gif') + '" alt="" />  ' + messages['common.please_wait'], {
        fontFamily: 'Arial', fontWeight: 'bold', fontSize: 12
    });
}

function unblockUI() {
    $.unblockUI({ fadeOut:false });
}